09. Hello App with Flask-SQLAlchemy - Part 1

Hello App with Flask-SQLAlchemy - Part 1

ND004 C01 L03 09 Hello App With Flask-SQLAlchemy

Resources

Task Description:

Before going further, you'll want to be sure you've got both Flask and Flask-SQLAlchemy installed via your terminal.

Note: If pip3 does not work, try pip .

Task List:

Task Feedback:

Great—then let's get started!

ND004 C01 L03 09.1 Hello App With Flask-SQLAlchemy

Initializing the app

app = Flask(__name__) sets the name of your app to the name of your module ("app" if "app.py" is the name of your file).

Using @app.route

@app.route('/')
def index():
  ...

In this case, @app.route is a Python decorator . Decorators take functions and returns another function, usually extending the input function with additional ("decorated") functionality. @app.route is a decorator that takes an input function index() as the callback that gets invoked when a request to route / comes in from a client.

See: this primer on decorators from Real Python .

Running the flask app

To start the server,

  • We run a flask app defined at app.py with FLASK_APP=app.py flask run
    • FLASK_APP must be set to the server file path with an equal sign in between. No spaces. FLASK_APP = app.py will not work. These flags have to be set exactly as expected, as FLAG=value .
  • To enable live reload, set export FLASK_ENV=development in your terminal session to enable debug mode, prior to running flask run . Or call it together with flask run:
  $ FLASK_APP=app.py FLASK_DEBUG=true flask run
Alternative approach to run a Flask app: using __main__

Instead of using $ flask run , we could have also defined a method

if __name__ == '__main__':
  app.run()

at the bottom of our app.py script, and then called $ python3 app.py in our terminal to invoke app.run() and run the flask app this way.

When we call a script this way, using $ python script.py , the script's __name__ gets set to __main__ by the Python interpreter, which then runs through executing all code found in the script. When it reaches the end, and finds if __name__ == 'main' , it evaluates this to True and therefore calls app.run() at the end, running the Flask app.

Both approaches to running your server are valid and neither way is better than the other.

Note: different versions of flask take FLASK_APP=app.py versus FLASK_APP=app , etc.

Check out the docs on the Flask CLI to understand the various options for pointing FLASK_APP to your flask application.

Let's try this! Options to follow along

Over the next 5 parts of this lesson, try following along and doing the steps of each exercise for yourself, so you can gain hands-on coding experience working with Flask, Postgres, and SQLAlchemy. You can try everything in the provided workspaces, or you can follow along using your own local environment (or try both, if you want extra practice!). You can find the solution code both in the workspaces, as well as in the walkthrough screencasts.

Workspace Option

Complete Exercise 1 in the interactive workspace below.

Exercise 1

  • Write a Flask App that outputs 'Hello World!' when you run the application.

  • Run the application using FLASK_APP=app flask run , in debug mode.

  • Try using the method outlined above to run the application using $ python3 app.py instead, using the if __name__ == '__main__': method.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter-lab
  • Opened files (when workspace is loaded): n/a